﻿using System.Windows.Forms;
using System.Drawing;
using System;

namespace Editor.Custom_Controls
{
    public class ScrollableImagePanel : Control
    {
        private Point currentScroll = Point.Empty;
        public Image Picture { get; set; }
        public int ScrollSize { get; set; }
        public Size ScrollableSize { get; set; }
        public bool IsScrollable { get; set; }

        public ScrollableImagePanel()
        {
            DoubleBuffered = true;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            if (!IsScrollable)
                ScrollableSize = Size;

            if (Picture != null && ScrollableSize != Size.Empty)
            {
                double wratio = (double)Width / ScrollableSize.Width;
                double hratio = (double)Height / ScrollableSize.Height;
                double wwratio = (double)Picture.Width / ScrollableSize.Width;
                double hhratio = (double)Picture.Height / ScrollableSize.Height;

                g.DrawImage(Picture, ClientRectangle, 
                    new Rectangle(
                        new Point(
                        (int)Math.Ceiling(currentScroll.X * ScrollSize * wwratio)
                        , (int)Math.Ceiling(currentScroll.Y * ScrollSize * hhratio)),
                    new Size(
                        (int)Math.Ceiling(Picture.Width * wratio),
                        (int)Math.Ceiling(Picture.Height * hratio))),
                        GraphicsUnit.Pixel);
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            Rectangle scrollUp = new Rectangle(new Point(Width / 4, 0),
                new Size(Width / 2, Height / 4));
            Rectangle scrollDown = new Rectangle(new Point(Width / 4, 3 *
                Height / 4), new Size(Width / 2, Height / 4));
            Rectangle scrollLeft = new Rectangle(new Point(0, Height / 4),
                new Size(Width / 4, Height / 2));
            Rectangle scrollRight = new Rectangle(new Point(3 * Width / 4,
                Height / 4), new Size(Width / 4, Height / 2));

            int horizontalScroll = (int)Math.Ceiling((double)(ScrollableSize.Width 
                - Width) / ScrollSize);
            int verticalScroll = (int)Math.Ceiling((double)(ScrollableSize.Height
                - Height) / ScrollSize);

            if (scrollUp.Contains(e.Location))
            {
                if (currentScroll.Y > 0)
                {
                    currentScroll.Y--;
                    Invalidate();
                }
            }
            else if (scrollDown.Contains(e.Location))
            {
                if (currentScroll.Y < verticalScroll)
                {
                    currentScroll.Y++;
                    Invalidate();
                }
            }
            else if (scrollRight.Contains(e.Location))
            {
                if (currentScroll.X < horizontalScroll)
                {
                    currentScroll.X++;
                    Invalidate();
                }
            }
            else if (scrollLeft.Contains(e.Location))
            {
                if (currentScroll.X > 0)
                {
                    currentScroll.X--;
                    Invalidate();
                }
            }

            /*
            if (ScrollSize > 0)
            {
                double ratio = (double)Width / Picture.Width;
                int scrollCount = (int)Math.Ceiling((Picture.Height * ratio 
                    - Height) / ScrollSize);

                if (e.Location.Y < Height / 2)
                {
                    if (currentScroll > 0)
                    {
                        currentScroll--;
                        Invalidate();
                    }
                }
                else
                {
                    if (currentScroll < scrollCount)
                    {
                        currentScroll++;
                        Invalidate();
                    }
                }
            }
             * */
        }
    }
}